
//
//  DYRegistViewController.m
//  DYHospital
//
//  Created by 胡金东 on 16/3/23.
//  Copyright © 2016年 胡金东. All rights reserved.
//

#import "DYRegistViewController.h"
#import <SMS_SDK/SMSSDK.h>
#import "UIView+Color.h"

@interface DYRegistViewController ()
@property (nonatomic,strong)UIButton *checkingBtn;
@property (nonatomic,strong)UITextField *phoneNumTextField;
@property (nonatomic,strong)UITextField *checkingTextField;
@property (nonatomic,strong)UITextField *passWordTextField;
@property (nonatomic,strong)UIAlertView *alert;
@property (nonatomic,strong)NSTimer *timer;


@end



@implementation DYRegistViewController{
    int seconds;
}

-(void)viewWillAppear:(BOOL)animated
{
    UIBarButtonItem *backBtnItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"back_Arrow"] style:UIBarButtonItemStylePlain target:self action:@selector(backBtnItemClick)];
    backBtnItem.tintColor = [UIColor blackColor];
    self.navigationItem.leftBarButtonItem = backBtnItem;
}
-(void)backBtnItemClick
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"注册";
    [self setText];
    self.view.backgroundColor = [UIColor lightGrayColor];
    
    
    //设置时间长
    seconds=60;
    

    }
-(void)setText
{
    self.phoneNumTextField = [[UITextField alloc]initWithFrame:CGRectMake(20, 64 + 25, [UIScreen mainScreen].bounds.size.width - 40, 35)];
    self.phoneNumTextField.placeholder = @" 请输入手机号";
    self.phoneNumTextField.backgroundColor = [UIColor whiteColor];
    [self.phoneNumTextField setRadius:5 borderWith:0 borderColor:[UIColor clearColor]];
    self.phoneNumTextField.keyboardType = UIKeyboardTypeDecimalPad;
    self.phoneNumTextField.clearButtonMode = UITextFieldViewModeUnlessEditing;
    [self.view addSubview:self.phoneNumTextField];
    
    self.checkingTextField = [[UITextField alloc]initWithFrame:CGRectMake(20, 64 + 70, [UIScreen mainScreen].bounds.size.width - 40, 35)];
    self.checkingTextField.placeholder = @" 请输入验证码";
    self.checkingTextField.backgroundColor = [UIColor whiteColor];
    self.checkingTextField.keyboardType = UIKeyboardTypeDecimalPad;
    self.checkingTextField.clearButtonMode = UITextFieldViewModeUnlessEditing;
    [self.checkingTextField setRadius:5 borderWith:0 borderColor:[UIColor clearColor]];
    [self.view addSubview:self.checkingTextField];
    
    self.checkingBtn = [[UIButton alloc]initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width - 150, 64 + 70, 130, 35)];
    [self.checkingBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
    self.checkingBtn.backgroundColor = [UIColor redColor];
    [self.checkingBtn setRadius:5 borderWith:0 borderColor:[UIColor clearColor]];
    [self.checkingBtn addTarget:self action:@selector(checkingBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.checkingBtn];
    
    self.passWordTextField = [[UITextField alloc]initWithFrame:CGRectMake(20, 64 + 115, [UIScreen mainScreen].bounds.size.width - 40, 35)];
    self.passWordTextField.placeholder = @" 请输入密码";
    self.passWordTextField.backgroundColor = [UIColor whiteColor];
    self.passWordTextField.clearButtonMode = UITextFieldViewModeUnlessEditing;
    [self.passWordTextField setRadius:5 borderWith:0 borderColor:[UIColor clearColor]];
    [self.view addSubview:self.passWordTextField];
    
    UIButton *referBtn = [[UIButton alloc]initWithFrame:CGRectMake(20, 64 + 190 , [UIScreen mainScreen].bounds.size.width - 40, 40)];
    [referBtn setTitle:@"提交" forState:UIControlStateNormal];
    referBtn.backgroundColor = [UIColor purpleColor];
    [referBtn addTarget:self action:@selector(referBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [referBtn setRadius:5 borderWith:0 borderColor:[UIColor clearColor]];
    [self.view addSubview:referBtn];

}
//获取验证码
-(void)checkingBtnClick
{
    //区号
    NSString* str2 = [NSString stringWithFormat:@"86"];
    if (self.phoneNumTextField.text.length == 11) {
        
        [SMSSDK getVerificationCodeByMethod:SMSGetCodeMethodSMS phoneNumber:self.phoneNumTextField.text zone:str2 customIdentifier:nil result:^(NSError *error) {
            if (!error)
            {
                NSLog(@"验证码发送成功");
                self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changeMin) userInfo:nil repeats:YES];
            }else{
                UIAlertView* alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"codesenderrtitle", nil) message:[NSString stringWithFormat:@"错误描述：%@",[error.userInfo objectForKey:@"getVerificationCode"]] delegate:self cancelButtonTitle:NSLocalizedString(@"sure", nil) otherButtonTitles:nil, nil];
                [alert show];
            }
        }];
        
    }else{
        self.alert=[[UIAlertView alloc]initWithTitle:@"提示" message:@"手机号长度不对" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
        [self.alert show];
        NSTimer *timer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeClick) userInfo:nil repeats:YES];
        
        
    }

}

-(void)changeMin
{
    if (seconds == 0) {
        [self.checkingBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
        self.checkingBtn.enabled = YES;
        self.checkingBtn.hidden = NO;
        [self.timer invalidate];
        seconds = 60;
        return;
    }
    self.checkingBtn.enabled = NO;
    [self.checkingBtn setTitle:[NSString stringWithFormat:@"%ds",seconds] forState:UIControlStateNormal];
    seconds --;
    

}

//提示框消失
-(void)timeClick
{
    [self.alert dismissWithClickedButtonIndex:0 animated:YES];

}


//注册按钮
-(void)referBtnClick
{
    NSString* str2 = [NSString stringWithFormat:@"86"];
    [SMSSDK commitVerificationCode:self.checkingTextField.text phoneNumber:self.phoneNumTextField.text zone:str2 result:^(NSError *error) {
        
        if (!error) {
            NSLog(@"验证成功");
        }
        else
        {
            NSLog(@"错误信息:%@",error);
        }
    }];

}
@end
